#!/bin/bash
function build_one
{

SYSTEMROOT=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
TOOLCHAIN_PREFIX=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64

CXX=$TOOLCHAIN_PREFIX/bin/$ARCH_TYPE$API-clang++
CC=$TOOLCHAIN_PREFIX/bin/$ARCH_TYPE$API-clang
echo "######start to configure ffmpeg"

./configure \
--prefix=$PREFIX \
--arch=$ARCH \
--target-os=linux \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN_PREFIX/bin/llvm- \
--sysroot=$SYSTEMROOT \
--cxx=$CXX \
--cc=$CC \
--nm=$TOOLCHAIN_PREFIX/bin/llvm-nm \
--extra-cxxflags="-D__thumb__ -fexceptions -frtti" \
--extra-cflags="$CFLAGS -Os -fPIC -DANDROID -Wfatal-errors -Wno-deprecated" \
--extra-ldflags="-L$SYSTEMROOT/usr/lib" \
--disable-shared \
--enable-static \
--enable-gpl \
--enable-version3 \
--enable-runtime-cpudetect \
--enable-small \
--enable-network \
--disable-iconv \
--enable-asm \
--enable-neon \
--enable-yasm \
--disable-encoders \
--enable-encoder=h263 \
--enable-encoder=libx264 \
--enable-encoder=aac \
--enable-encoder=mpeg4 \
--enable-encoder=mjpeg \
--enable-encoder=png \
--enable-encoder=gif \
--enable-encoder=bmp \
--disable-muxers \
--enable-muxer=h264 \
--enable-muxer=flv \
--enable-muxer=gif \
--enable-muxer=mp3 \
--enable-muxer=dts \
--enable-muxer=mp4 \
--enable-muxer=mov \
--enable-muxer=mpegts \
--disable-decoders \
--enable-decoder=aac \
--enable-decoder=aac_latm \
--enable-decoder=mp3 \
--enable-decoder=h263 \
--enable-decoder=h264 \
--enable-decoder=mpeg4 \
--enable-decoder=mjpeg \
--enable-decoder=gif \
--enable-decoder=png \
--enable-decoder=bmp \
--enable-decoder=yuv4 \
--disable-demuxers \
--enable-demuxer=image2 \
--enable-demuxer=h263 \
--enable-demuxer=h264 \
--enable-demuxer=flv \
--enable-demuxer=gif \
--enable-demuxer=aac \
--enable-demuxer=ogg \
--enable-demuxer=dts \
--enable-demuxer=mp3 \
--enable-demuxer=mov \
--enable-demuxer=m4v \
--enable-demuxer=concat \
--enable-demuxer=mpegts \
--enable-demuxer=mjpeg \
--enable-demuxer=mpegvideo \
--enable-demuxer=rawvideo \
--enable-demuxer=yuv4mpegpipe \
--enable-demuxer=rtsp \
--disable-parsers \
--enable-parser=aac \
--enable-parser=ac3 \
--enable-parser=h264 \
--enable-parser=mjpeg \
--enable-parser=png \
--enable-parser=bmp\
--enable-parser=mpegvideo \
--enable-parser=mpegaudio \
--disable-protocols \
--enable-protocol=file \
--enable-protocol=hls \
--enable-protocol=concat \
--enable-protocol=rtp \
--enable-protocol=rtmp \
--enable-protocol=rtmpt \
--disable-filters \
--disable-filters \
--enable-filter=aresample \
--enable-filter=asetpts \
--enable-filter=setpts \
--enable-filter=ass \
--enable-filter=scale \
--enable-filter=concat \
--enable-filter=atempo \
--enable-filter=movie \
--enable-filter=overlay \
--enable-filter=rotate \
--enable-filter=transpose \
--enable-filter=hflip \
--disable-outdevs \
--disable-doc \
--disable-ffplay \
--disable-ffmpeg \
--disable-debug \
--disable-ffprobe \
--disable-postproc \
--enable-avdevice \
--disable-symver \
--enable-zlib \
--disable-stripping

echo "######start making $CPU"

make clean
make -j8
make install

}

# 工具安装
apt-get install -y unzip
cd ..
PWDPATH=$(pwd)
# ndk 主目录
ANDROID_NDK=$PWDPATH/android/ndk/android-ndk-r21e
# 解压 ndk 工具
cd $PWDPATH/android/ndk
rm -rf $ANDROID_NDK
unzip $PWDPATH/tar/android-ndk-r21e-linux-x86_64.zip -d ./



FFMPEG_ZX=$PWDPATH/tar/ffmpeg-6.1.2.tar.xz
FFMPEG_Code=ffmpeg-6.1.2

API=21
# arm-v7a
cd $PWDPATH/android/build
rm -rf $FFMPEG_Code
tar -xvf $FFMPEG_ZX
cd $FFMPEG_Code
CPU=armv7-a
PREFIX=$PWDPATH/android/lib/$CPU
ARCH=arm
ARCH_TYPE=armv7a-linux-androideabi
CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon"
build_one


# arm64
cd $PWDPATH/android/build
rm -rf $FFMPEG_Code
tar -xvf $FFMPEG_ZX
cd $FFMPEG_Code
CPU=armv8-a
PREFIX=$PWDPATH/android/lib/$CPU
ARCH=arm64
CFLAGS="-march=armv8-a"
ARCH_TYPE=aarch64-linux-android
build_one

# 清理编译文件
rm -rf ../$FFMPEG_Code
rm -rf $ANDROID_NDK